const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');
var dateTimePicker = require('../../utils/dateTimePicker.js');
var app = getApp();

Page({
  data: {
    showRegistrate: false,
    taskNumber: '062469f845a94da2a3976fae989e16d5',
    receiverPhone: '13235125574',
    registrate: 1, //1 发货签收  2 收货签收
    showQuery: false, //冷箱历史温控查询
    codeNos: [], //冷箱编号
    selectColdNo: '请选择冷箱编号', //选中的冷箱编号
    missionNos: [], //任务号
    selectMissionNo: '请选择任务号', //选中的任务号

    startTime: '开始时间',
    endTime: '结束时间',

    dateTimeArray1: null,
    dateTime1: null,
    dateTime2: null,
    startYear: 2020,
    endYear: 2030,
  },

  onLoad: function (options) {
    let that = this;

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    //var lastArray = obj1.dateTimeArray.pop();
    //var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTimeArray1: obj1.dateTimeArray,
    });


  },

  // 选择日期
  changeDateTime1(e) {
    console.log(e.detail.value)
    this.setData({
      dateTime1: e.detail.value,
    });
    this.setData({
      startTime: this.data.dateTimeArray1[0][this.data.dateTime1[0]] +
        '-' + this.data.dateTimeArray1[1][this.data.dateTime1[1]] +
        '-' + this.data.dateTimeArray1[2][this.data.dateTime1[2]] +
        ' ' + this.data.dateTimeArray1[3][this.data.dateTime1[3]] +
        ':' + this.data.dateTimeArray1[4][this.data.dateTime1[4]]
    })
  },


  // 选择时间
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1,
      dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

  // 选择日期
  changeDateTime2(e) {
    this.setData({
      dateTime2: e.detail.value
    });

    this.setData({
      endTime: this.data.dateTimeArray1[0][this.data.dateTime2[0]] +
        '-' + this.data.dateTimeArray1[1][this.data.dateTime2[1]] +
        '-' + this.data.dateTimeArray1[2][this.data.dateTime2[2]] +
        ' ' + this.data.dateTimeArray1[3][this.data.dateTime2[3]] +
        ':' + this.data.dateTimeArray1[4][this.data.dateTime2[4]]
    })
  },

  //取消
  cancelTap: function () {
    this.setData({
      showRegistrate: false
    })
  },

  //确定
  sureTap: function () {

    this.setData({
      showRegistrate: false
    })
    if (this.data.registrate == 2) {
      this.getGoodsList()
    } else {
      this.getWarehouseGoodsList()
    }
  },


  //查询取消
  cancelTap2: function () {
    this.setData({
      showQuery: false
    })
  },

  //查询确定
  sureTap2: function () {
   
    if(this.data.selectColdNo =='请选择冷箱编号'){
      wx.showToast({
        title: '请选择冷箱编号',
      })
      return
    }

    if(this.data.selectMissionNo =='请选择任务号'){
      wx.showToast({
        title: '请选择任务号',
      })
      return
    }

    if(this.data.startTime =='开始时间'){
      wx.showToast({
        title: '请选择开始时间',
      })
      return
    }

    if(this.data.endTime =='结束时间'){
      wx.showToast({
        title: '请选择结束时间',
      })
      return
    }
    this.setData({
      showQuery: false
    })

    wx.navigateTo({
      url: '../ColdBoxHistoryTemControlDetails/ColdBoxHistoryTemControlDetails?coldBoxId='+this.data.selectColdNo+'&waybillNumber='+this.selectMissionNo+'&startTime='+this.data.startTime+'&endTime='+this.data.endTime,
    })

   
  },

   //任务号
   missionCodeInput: function (e) {
    this.setData({
      taskNumber: e.detail.value
    })
  },
  //手机号
  missionCodeInput: function (e) {
    this.setData({
      receiverPhone: e.detail.value
    })
  },

  //任务号
  bindMissionNoChange: function (e) {
    this.setData({
      selectMissionNo: this.data.missionNos[e.detail.value].id
    })
  },
  //冷箱编号
  bindColdNoChange: function (e) {
    this.setData({
      selectColdNo:this.data.codeNos[e.detail.value].id
    })
  },

  // 绑车管理
  toDeviceManage: function () {
    wx.navigateTo({
      url: '../deviceManage/deviceManage',
    })
  },

  // 冷箱历史温控
  toQuery: function () {
    if (this.showQuery) {
      this.setData({
        showQuery: false
      })
    } else {
      this.setData({
        showQuery: true
      })
    }
    if (this.data.missionNos.length == 0) {
      this.getBoxWaybillNumberList() //任务号列表
      this.getBoxList() //冷箱列表
    }

  },

  // 实时报警提醒
  toRealTimeAlarm: function () {
    wx.navigateTo({
      url: '../transRealTimeAlarm/transRealTimeAlarm',
    })
  },

  //收货签收
  toReceiveRegistrate: function () {
    this.setData({
      showRegistrate: true,
      registrate: 2
    })
  },

  //发货签收
  toSendRegistrate: function () {
    this.setData({
      showRegistrate: true,
      registrate: 1
    })
  },

  //车辆实时温控
  toRealTime: function () {
    wx.navigateTo({
      url: '../realTimeTempControl/realTimeTempControl',
    })
  },

  //车辆历史温控
  toHistoryTemp: function () {
    wx.navigateTo({
      url: '../historyTemControl/historyTemControl',
    })
  },

  //任务监控
  toTransMonitor: function () {
    wx.navigateTo({
      url: '../transMonitor/transMonitor',
    })
  },

  //冷箱实时温控
  toColdBoxReal: function () {
    wx.navigateTo({
      url: '../coldBoxRealTimeTempControl/coldBoxRealTimeTempControl',
    })
  },

  //任务号
  missionCodeInput: function (e) {
    this.setData({
      taskNumber: e.detail.value
    })
  },

  //手机号
  phoneInput: function (e) {
    this.setData({
      receiverPhone: e.detail.value
    })
  },



  //获取商品清单
  getGoodsList: function () {

    if (this.data.taskNumber == '') {
      wx.showToast({
        title: '请输入任务号',
      })
      return
    }

    if (this.data.receiverPhone == '') {
      wx.showToast({
        title: '请输入手机号',
      })
      return
    }

    let data = {
      taskNumber: this.data.taskNumber,
      receiverPhone: this.data.receiverPhone
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.getGoodsList,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null) {
            wx.navigateTo({
              url: '../../pages/receiveRegistrate/receiveRegistrate?taskNumber=' + that.data.taskNumber + '&receiverPhone=' + that.data.receiverPhone + '&goods=' + res.data + '&type=1',
            })
          } else {
            wx.showToast({
              title: '任务号不存在',
            })
          }
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

  //获取商品清单
  getWarehouseGoodsList: function () {

    if (this.data.taskNumber == '') {
      wx.showToast({
        title: '请输入任务号',
      })
      return
    }

    let data = {
      taskNumber: '195030fbe6644adea08434a57943c2f9' //this.data.taskNumber,
    }
    const that = this
    wx.showLoading()
    wxHttpHandler({
      type: 'get',
      path: api.getWarehouseGoodsList,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null) {
            wx.navigateTo({
              url: '../../pages/receiveRegistrate/receiveRegistrate?taskNumber=' + that.data.taskNumber + '&goods=' + res.data + '&type=2',
            })
          } else {
            wx.showToast({
              title: '任务号不存在',
            })
          }
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },


  //冷箱编号列表
  getBoxList: function () {
    let data = {}
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.getBoxList + wx.getStorageSync('id'),
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null) {
            that.setData({
              codeNos: res.data
            })
          }
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

  //运输任务号列表
  getBoxWaybillNumberList: function () {
    let data = {}
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.getBoxWaybillNumberList + wx.getStorageSync('id'),
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null) {
            that.setData({
              missionNos: res.data
            })
          }
          wx.hideLoading()
        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },

})